//
//  YWInformationBankCell.m
//  maintain
//
//  Created by Kyo on 16/5/19.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "Masonry.h"
#import "YWInformationBankCell.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "YWInformationBankModel.h"
#import "YWattachment.h"
#import "YWHandler.h"
#import "YWCommonTool.h"
#import "YWAttachmentsView.h"

@interface YWInformationBankCell ()
@property (nonatomic, weak)UIImageView *iconImageView;
@property (nonatomic, weak)UILabel *titleLabel;
@property (nonatomic, weak)UILabel *typeLabel;
@property (nonatomic, weak)UILabel *fromeLabel;
@property (nonatomic, weak)UILabel *nameLabel;
@property (nonatomic, weak)UILabel *timeLabel;

@end

@implementation YWInformationBankCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"YWInformationBankCelldentifier";
    YWInformationBankCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWInformationBankCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self autolayoutViews];
    }
    return self;
}

-(void)setInformationModel:(YWInformationBankModel *)informationModel{
    _informationModel = informationModel;
    NSString *typemName = [[informationModel.attachment.downLoadUrl componentsSeparatedByString:@"."] lastObject];
    self.iconImageView.image = [YWAttachmentsView imageWithType:typemName];
    self.titleLabel.text = informationModel.name;
    if (informationModel.typeId == YWInformationTypePlan) {
        self.typeLabel.text = @"方案";
    }else{
        self.typeLabel.text = @"报告";
    }
    self.nameLabel.text = informationModel.creator.userName;
    NSString *timeString = [YWCommonTool showTimeWithNormalFormatter:informationModel.createTime/1000];
    NSString *sizeStr = [YWCommonTool getFileSize:informationModel.attachment.fileSize*1024];
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@",timeString,sizeStr];
}

- (void)autolayoutViews{
    CGFloat iconSize = 40;
    CGFloat typeLabelW = 30;
    CGFloat fromeLabelW = 40;
    CGFloat titleLabelW = SCREEN_WIDTH - iconSize - typeLabelW - kMarginW*4;
    CGFloat nameLabelW = titleLabelW - fromeLabelW;
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(kTopPadding*2);
        make.left.mas_equalTo(self.contentView.mas_left).offset(kMarginW);
        make.width.mas_equalTo(iconSize);
        make.height.mas_equalTo(iconSize);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(kTopPadding);
        make.left.mas_equalTo(self.contentView.mas_left).offset(kMarginW*2+iconSize);
        make.width.mas_equalTo(titleLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.titleLabel.mas_top);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(kMarginW);
        make.width.mas_equalTo(typeLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.fromeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
        make.left.mas_equalTo(self.titleLabel.mas_left);
        make.width.mas_equalTo(fromeLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.fromeLabel.mas_top);
        make.left.mas_equalTo(self.fromeLabel.mas_right);
        make.width.mas_equalTo(nameLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.fromeLabel.mas_bottom);
        make.left.mas_equalTo(self.fromeLabel.mas_left);
        make.width.mas_equalTo(titleLabelW+kMarginW+typeLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
}
- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        _iconImageView = imageView;
        [self.contentView addSubview:imageView];
    }
    return _iconImageView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = KTitleFont;
        [self.contentView addSubview:titleLabel];
        _titleLabel = titleLabel;
//        titleLabel.backgroundColor = [UIColor redColor];
    }
    return _titleLabel;
}
-(UILabel *)typeLabel{
    if (!_typeLabel) {
        UILabel *typeLabel = [[UILabel alloc]init];
        typeLabel.font = kContentFont;
        typeLabel.textColor = [UIColor customBlueColor];
        [self.contentView addSubview:typeLabel];
        _typeLabel = typeLabel;
//        typeLabel.backgroundColor = [UIColor yellowColor];
    }
    return _typeLabel;
}
- (UILabel *)fromeLabel{
    if (!_fromeLabel) {
        UILabel *fromeLabel = [[UILabel alloc]init];
        fromeLabel.font = kContentFont;
        fromeLabel.text = @"来自: ";
        [self.contentView addSubview:fromeLabel];
        _fromeLabel = fromeLabel;
//        fromeLabel.backgroundColor = [UIColor grayColor];
    }
    return _fromeLabel;
}
-(UILabel *)nameLabel{
    if (!_nameLabel) {
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = kContentFont;
        nameLabel.textColor = [UIColor customBlueColor];
        [self.contentView addSubview:nameLabel];
        _nameLabel = nameLabel;
//        nameLabel.backgroundColor = [UIColor greenColor];
    }
    return _nameLabel;
}
-(UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = kContentFont;
        timeLabel.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:timeLabel];
        _timeLabel = timeLabel;
//        timeLabel.backgroundColor = [UIColor purpleColor];
    }
    return _timeLabel;
}
@end
